<?php

namespace app\admin\controller;

use app\admin\model\FilmstripModel;
use think\Db;

class Filmstrip extends Base
{

    /**
     * [index 幻灯片列表]
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['create_time'] = ['between time',[$start,$end]];
            }
            $field=input('field');//字段
            $order=input('order');//排序方式
            if($field && $order){
                $od=$field." ".$order;
            }else{
                $od="create_time desc";
            }
            $filmstrip = new FilmstripModel();
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $count = $filmstrip->getAllFilmstrip($map);  //总数据
            $lists = $filmstrip->getFilmstripByWhere($map, $Nowpage, $limits,$od);
            for($i=0;$i<count($lists);$i++){
                $img = explode(',',$lists[$i]['img']);
                $lists[$i]['img'] = array_values($img);
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch("filmstrip/index");
    }

    /**
     * [add_filmstrip 添加幻灯片]
     * @return mixed|\think\response\Json
     */
    public function add_filmstrip()
    {
        if(request()->isPost()){
            extract(input());
            $param = input('post.');
            $filmstrip = new FilmstripModel();
            $flag = $filmstrip->insertFilmstrip($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch('filmstrip/add_filmstrip',['configState'=>config('qiniu_allow_state')]);
    }

    /**
     * [edit_filmstrip 编辑幻灯片]
     * @return mixed|\think\response\Json
     */
    public function edit_filmstrip()
    {
        $filmstrip = new FilmstripModel();
        if(request()->isPost()){
            $param = input('post.');
            $flag = $filmstrip->editFilmstrip($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $filmstrip = new FilmstripModel();
        $data = $filmstrip->getOneFilmstrip($id);
        if(!empty($data['img'])){
            $data['img'] = trim($data['img'],',');
        }else{
            $data['img'] = '';
        }
        return $this->fetch('filmstrip/edit_filmstrip',['filmstrip'=>$data,'configState'=>config('qiniu_allow_state')]);
    }

    /**
     * [del_filmstrip 删除幻灯片]
     * @return mixed|\think\response\Json
     */
    public function del_filmstrip()
    {
        $id = input('param.id');
        $filmstrip = new FilmstripModel();
        $flag = $filmstrip->delFilmstrip($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * editField 快捷编辑
     * @return \think\response\Json
     */
    public function editField(){
        extract(input());
        $res = Db::name($table)->where(['id' => $id ])->setField($field , $value);
        if($res){
            writelog('更新字段成功',200);
            return json(['code' => 200,'data' => '', 'msg' => '更新字段成功']);
        }else{
            writelog('更新字段失败',100);
            return json(['code' => 100,'data' => '', 'msg' => '更新字段失败']);
        }
    }

    /**
     * [filmstrip_state 幻灯片状态]
     * @return \think\response\Json
     */
    public function filmstrip_state()
    {
        extract(input());
        $filmstrip = new FilmstripModel();
        $flag = $filmstrip->filmstripState($id,$num);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * [batchDelFilmstrip 批量删除幻灯片]
     * @return \think\response\Json
     */
    public function batchDelFilmstrip(){
        extract(input());
        if(empty($ids)){
            return json(['code'=>100,'msg'=>'请选择要删除的记录！']);
        }
        $filmstrip = new FilmstripModel();
        $flag = $filmstrip->batchDelFilmstrip($ids);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    /**
     * [usingFilmstrip 批量启用幻灯片]
     * @return \think\response\Json
     */
    public function usingFilmstrip(){
        extract(input());
        $list = [];
        if($ids){
            $ids = explode(',',$ids);
            for($i=0;$i<count($ids);$i++){
                $param = [
                    'id'=>$ids[$i],
                    'status'=>1
                ];
                $list[] = $param;
            }
        }
        $filmstrip = new FilmstripModel();
        $flag = $filmstrip->usingFilmstrip($list);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    /**
     * [forbiddenFilmstrip 批量禁用幻灯片]
     * @return \think\response\Json
     */
    public function forbiddenFilmstrip(){
        extract(input());
        $list = [];
        if($ids){
            $ids = explode(',',$ids);
            for($i=0;$i<count($ids);$i++){
                $param = [
                    'id'=>$ids[$i],
                    'status'=>2
                ];
                $list[] = $param;
            }
        }
        $filmstrip = new FilmstripModel();
        $flag = $filmstrip->forbiddenFilmstrip($list);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    public function printOrder(){
        return $this->fetch('article/order');
    }
}